package com.edgclub.edg.activity;


import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.edgclub.edg.R;
import com.edgclub.edg.api.ApiConfig;
import com.edgclub.edg.utils.util.StringUtils;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;
import com.umeng.update.UpdateStatus;


public class SettingAboutActivity extends GzBaseTooolBarActivity implements View.OnClickListener {
    private TextView versionName;
    private LinearLayout update_gz;
    private RelativeLayout tweet , comment;
    @Override
    public int getContentView() {
        return R.layout.about_activity;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("关于EDG");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
    }

    @Override
    public void initView() {
        update_gz = (LinearLayout) findViewById(R.id.update_gz);
        versionName =  (TextView) findViewById(R.id.versionName);
        tweet = (RelativeLayout) findViewById(R.id.tweet);
        comment = (RelativeLayout) findViewById(R.id.comment);
        tweet.setOnClickListener(this);
        versionName.setText(getVersion(this));
        update_gz.setOnClickListener(this);
        comment.setOnClickListener(this);
        findViewById(R.id.user_protocol).setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_back:
                finish();
                break;
            case R.id.comment:
                goMarket(this);
                break;
            case R.id.update_gz:
//			UmengUpdateAgent.forceUpdate(this);

                UmengUpdateAgent.setUpdateAutoPopup(false);
                UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
                    @Override
                    public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
                        switch (updateStatus) {
                            case UpdateStatus.Yes: // has update
                                UmengUpdateAgent.showUpdateDialog(SettingAboutActivity.this, updateInfo);
                                break;
                            case UpdateStatus.No: // has no update
                                Toast.makeText(SettingAboutActivity.this, StringUtils.getString(R.string.isnew), Toast.LENGTH_SHORT).show();
                                break;
                            case UpdateStatus.NoneWifi: // none wifi
                                UmengUpdateAgent.showUpdateDialog(SettingAboutActivity.this, updateInfo);
                                break;
                            case UpdateStatus.Timeout: // time out
                                Toast.makeText(SettingAboutActivity.this, StringUtils.getString(R.string.isdelay), Toast.LENGTH_SHORT).show();
                                break;
                        }
                    }
                });
                UmengUpdateAgent.update(this);
                break;
            case R.id.tweet:
                Intent i=new Intent(this,GzWebViewActivity.class);
                i.putExtra("url", "http://weibo.com/edgteam");
                i.putExtra("actbarTitle","官方微博");
                i.putExtra("right_btn",true);
                startActivity(i);
                break;

            case R.id.user_protocol:
                Intent intent=new Intent(this,GzWebViewActivity.class);
                intent.putExtra("url", ApiConfig.USER_PROTOCOL_URL);
                intent.putExtra("right_btn",true);
                startActivity(intent);
                break;
        }
    }
    //获取版本号
    public  String getVersion(Context context)  {
        try {
            PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 跳转到市场
     *
     * @param activity
     */
    public static void goMarket(Activity activity) {
        Uri uri = Uri.parse("market://details?id=com.edgclub.edg");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            activity.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(activity, StringUtils.getString(R.string.activitycontent1), Toast.LENGTH_LONG)
                    .show();
        }
    }
}
